package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.domain.exception.*;

/**
 * Class provides bank service functionality
 * @author alex
 */
public class BankService {
	private Bank bank;
	private BankLoader bankLoader;
	
	/**
	 * @param bank the bank
	 */
	public BankService(Bank bank) {
		this.bank = bank;
		this.bankLoader = new BankLoader(bank);
	}
	
	/**
	 * @param client the client
	 * @throws com.acme.bankapp.domain.exception.ClientExistsException
	 */
	public void addClient(Client client) throws ClientExistsException {
		bank.addClient(client);
		bankLoader.saveBank();
	}
	
	/**
	 * @param client the client
	 * @param account the account
	 * @throws com.acme.bankapp.domain.exception.ClientNotFoundException
	 */
	public void addClientAccount(Client client, Account account) throws ClientNotFoundException {
		bank.getClient(client).addAccount(account);
		bankLoader.saveBank();
	}
	
	/**
	 * @param client the client
	 * @param accountId the account id
	 * @return the client account
	 * @throws com.acme.bankapp.domain.exception.ClientNotFoundException
	 * @throws com.acme.bankapp.domain.exception.AccountNotFoundException
	 */
	public Account getClientAccount(Client client, int accountId) throws AccountNotFoundException, ClientNotFoundException {
		return bank.getClient(client).getAccount(accountId);
	}
	
	/**
	 * @param client the client
	 * @param accountId the account id
	 * @param x the money
	 * @throws com.acme.bankapp.domain.exception.ClientNotFoundException
	 * @throws com.acme.bankapp.domain.exception.AccountNotFoundException
	 * @throws com.acme.bankapp.domain.exception.NotEnoughFundsException
	 */
	public void withdrawClientAccount(Client client, int accountId, double x) throws NotEnoughFundsException, AccountNotFoundException, ClientNotFoundException {
		this.getClientAccount(client, accountId).withdraw(x);
		bankLoader.saveBank();
	}
	
	/**
	 * @param client the client
	 * @param accountId the account id
	 * @param x the money
	 * @throws com.acme.bankapp.domain.exception.ClientNotFoundException
	 * @throws com.acme.bankapp.domain.exception.AccountNotFoundException
	 */
	public void depositClientAccount(Client client, int accountId, double x) throws AccountNotFoundException, ClientNotFoundException {
		this.getClientAccount(client, accountId).deposit(x);
		bankLoader.saveBank();
	}
	
	/**
	 * @param client the client
	 * @param accountId the account id
	 * @return the maximum amount to withdraw
	 * @throws com.acme.bankapp.domain.exception.ClientNotFoundException
	 * @throws com.acme.bankapp.domain.exception.AccountNotFoundException
	 */
	public double getMaximumAmountToWithdraw(Client client, int accountId) throws AccountNotFoundException, ClientNotFoundException {
		return this.getClientAccount(client, accountId).maximumAmountToWithdraw();
	}
}
